package com.liuanbang.saro.sarodemo.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.liuanbang.saro.sarodemo.R;
import com.liuanbang.saro.sarodemo.base.BaseActivity;
import com.liuanbang.saro.sarodemo.ui.items.annotationdemo.Description;
import com.liuanbang.saro.sarodemo.ui.items.annotationdemo.Name;
import com.liuanbang.saro.sarodemo.ui.items.annotationdemo.TableDemo.Column;
import com.liuanbang.saro.sarodemo.ui.items.annotationdemo.TableDemo.Person;
import com.liuanbang.saro.sarodemo.ui.items.annotationdemo.TableDemo.Person2;
import com.liuanbang.saro.sarodemo.ui.items.annotationdemo.TableDemo.Table;
import com.liuanbang.saro.sarodemo.util.ToastUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by 刘安邦 on 2017/2/28.
 * Email:liuanbang@aliyun.com
 * 描述:       2017/2/28
 * 修改:
 */

public class AnnotationDemoActivity extends BaseActivity {
    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_interface_demo);

        //demo1();
        demo2();
        ToastUtils.showToast("请查看日志log");
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public Toolbar getToolbar() {
        return null;
    }

    private void demo1() {

        //使用反射加载类
        try {
            //反射方法一 (推荐使用)
            Class c = Class.forName("com.liuanbang.saro.sarodemo.ui.items.annotationdemo.China");
            //反射方法二
            //Class c = China.class;
            //反射方法三
            /*China china = new China();
            Class c = china.getClass();*/
            if (c.isAnnotationPresent(Name.class)) {//判断是否包含该注解类
                Name name = (Name) c.getAnnotation(Name.class);
                Log.i(TAG, "initView: " + name.value());
            }

            //------------解析方法一--------------
            //获取所有方法
            Method[] m = c.getDeclaredMethods();
            for (Method method : m) {
                if (method.isAnnotationPresent(Description.class)) {
                    //获取方法上注解类实例
                    Description description = method.getAnnotation(Description.class);
                    Log.i(TAG, "initView: " + description.name() + description.sex() + description.age());
                }
            }

            //-----------解析方法二------------
            for (Method method : m) {
                Annotation[] as = method.getAnnotations();
                for (Annotation a : as) {
                    if (a instanceof Description) {
                        Log.i(TAG, "initView: " + ((Description) a).age() + ((Description) a).sex() + ((Description) a).name());
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询Demo
    private void demo2() {
        Person p1 = new Person();
        p1.setId(10);

        Person p2 = new Person();
        p2.setUserName("王朔");

        Person p3 = new Person();
        p3.setEmail("wangf@126.com,yhwqeui@qq.com,haha@aliyun.com");

        Person2 p4 = new Person2();
        p4.setCity1("山海");
        p4.setMobile1("12345667788");
        p4.setNickName1("就哈哈");

        String sq1 = query(p1);
        String sq2 = query(p2);
        String sq3 = query(p3);
        String sq4 = query(p4);

        Log.i(TAG, "demo2: " + sq1 + "\n" + sq2 + "\n" + sq3 + "\n" + sq4);
    }

    private static String query(Object p) {
        //拼接字符串 尽量使用StringBuilder
        StringBuilder sb = new StringBuilder();
        //1获取到Class
        Class c = p.getClass();
        //2.获取到table的名字
        //首先判断是否带有注解  不带有 则直接返回null
        if (!c.isAnnotationPresent(Table.class)) {
            return null;
        }

        Table t = (Table) c.getAnnotation(Table.class);
        String tableName = t.value();
        sb.append("select * from ").append(tableName).append(" where 1 = 1");

        //3 遍历所有字段
        Field[] fArray = c.getDeclaredFields();
        Object filedValue = null;
        for (Field f : fArray) {
            //4.首先拿到字段名字
            if (!f.isAnnotationPresent(Column.class)) {
                continue;
            }
            Column column = f.getAnnotation(Column.class);
            String columnName = column.value();
            //拿到当前字段的值
            String filedName = f.getName();
            String getMethodName = "get" + filedName.substring(0, 1).toUpperCase() + filedName.substring(1);

            //通过反射调用方法
            try {
                Method getMethod = c.getMethod(getMethodName);
                // TODO: 2017/2/28 通过方法调用
                //invoke(类,参数)
                filedValue = getMethod.invoke(p);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //拼接sql
            //去除空
            if (filedValue == null || (filedValue instanceof Integer && (Integer) filedValue == 0)) {
                continue;
            }
            if (filedValue instanceof String) {
                if (((String) filedValue).contains(",")) {
                    String[] values = ((String) filedValue).split(",");
                    sb.append(" and ").append(filedName).append(" in (");
                    for (String s : values) {
                        sb.append("'").append(s).append("'").append(",");
                    }

                    sb.deleteCharAt(sb.length() - 1).append(")");
                } else {
                    sb.append(" and ").append(filedName).append(" = ").append("'").append(filedValue).append("'");
                }
            } else if (filedValue instanceof Integer) {
                sb.append(" and ").append(filedName).append(" = ").append(filedValue);
            }
        }
        return sb.toString();
    }


}
